import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFValue } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { PartnerListService } from '../../services/partner-list.service';

@Component({
  selector: 'app-partner-audit-modal',
  templateUrl: './partner-audit-modal.component.html',
  styleUrls: ['./partner-audit-modal.component.less']
})
export class PartnerAuditModalComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @Input()
  info: any;
  schema!: SFSchema;
  formData: any;
  sourcePage = '';
  constructor(private nzModalService: NzModalService, public service: PartnerListService, private modal: NzModalRef) {}

  ngOnInit(): void {
    console.log(this.info);
    this.initSF(this.info);
    // asyncData: () => this.service.getChannel(),

    if(this.info.channelId) {
      const value = [{
        label: this.info.channelIdLabel,
        value: this.info.channelSaleslId,
      }]
      setTimeout(() => {
        if(this.sf) {
          this.sf.getProperty('/channelId')!.schema.enum = value;
          this.sf.getProperty('/channelId')!.widget.reset(value);
          this.sf.setValue('/channelId', this.info.channelSaleslId);
         }
      })

    } else {
      this.service.getChannel().subscribe((res) => {
        console.log(res);
        const value :any= res;
        setTimeout(() => {
          if(this.sf) {
            this.sf.getProperty('/channelId')!.schema.enum = value;
            this.sf.getProperty('/channelId')!.widget.reset(value);
            this.sf.setValue('/channelId', value);
           }
        })
      })
    
    }
  }

  initSF(user: any) {
    this.schema = {
      properties: {
        isPass: {
          type: 'boolean',
          ui: {
            hidden: true
          },
          default: this.info.isPass
        },
        staffName: {
          title: '合伙人名称',
          type: 'string',
          ui: { widget: 'text' },
          default: user.enterpriseName
        },
        status: {
          title: '审核结果',
          type: 'string',
          maxLength: 11,
          enum: [
            { value: true, label: '通过' },
            { value: false, label: '驳回' }
          ],
          ui: {
            widget: 'radio',
            hidden: this.info.isPass !== undefined
          },
          default: this.info.isPass !== undefined ? this.info.isPass : true
        },
        channelId: {
          title: '渠道销售',
          type: 'string',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            hidden: this.info.isPass === false,
            visibleIf: {
              status: value => value
            },
            errors: {
              required: ' '
            }
          },
          default: user.channelId
        },
        approvalOpinion: {
          title: '备注',
          type: 'string',
          maxLength: 100,
          ui: {
            widget: 'textarea',
            placeholder: '请不要超过100个字',
            autosize: { minRows: 3 },
            hidden: this.info.isPass === true,
            visibleIf: {
              status: value => !value || this.info.isPass === false
            },
            errors: {
              required: ' '
            }
          }
        }
      },
      required: ['channelId', 'approvalOpinion']
    };
    console.log('666');
    
  }

  sure() {
    if (this.info.isPass || this.sf?.value.status) {
      this.nzModalService.confirm({
        nzTitle: `确定以“${this.info.channelIdLabel}”的名义重新发起CRM《付款对象合同管理》吗？`,
        nzOnOk: () => {
          this.audit();
        }
      });
    } else {
      this.audit();
    }
  }

  audit() {
    const params: any = {
      ...this.sf?.value,
      id: this.info.id,
      auditStatusEnum: this.info.isPass || this.sf?.value.status ? '20' : '30'
    };
    this.service
      .request(this.service.$api_audit_partner, {
        ...params,
        auditStatusEnum: params.auditStatusEnum,
        id: params.id,
        approvalOpinion: params.approvalOpinion,
        sourcePage: this.sourcePage
      })
      .subscribe(res => {
        if (res) {
          this.service.msgSrv.success('审核成功！');
          this.modal.destroy(1);
        }
      });
  }

  close() {
    this.nzModalService.closeAll();
  }
}
